home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / msed.zip / MS_SSED / MS_SSED.C < prev    next >
C/C++ Source or Header  |  1992-11-08  |  16KB  |  654 lines

  1. extern void found( int , char *);
  2. #define TCHAR char
  3. #define LEN 256
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <malloc.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include "myopt.h"
  11. #define TRUE 1
  12. #define FALSE 0
  13.  
  14. #define MAX_NUMBER 100
  15. #define MAX_PARMS    8
  16.  
  17. #define max(a,b) (((a) > (b)) ? (a) : (b))
  18.  
  19. struct _apitype {
  20.     char *pchName;
  21.     char *pchformat;
  22.     int   cb;
  23.     int   cParms;
  24.     int   iOrder[MAX_PARMS];
  25. } apilist[MAX_NUMBER];
  26. int api_num = -1;
  27.  
  28.  
  29. struct _apidump
  30.     {
  31.     char  pchName[256];
  32.     char  pchformat[256];
  33.     int   cb;
  34.     int   cParms;
  35.     int   iOrder[MAX_PARMS];
  36.     } apidump;
  37.  
  38.  
  39. FILE *fp_src, *fp_dst, *fp_err, *fp_dat;
  40. char chFirstBuf[53];
  41. int token_length = 0;
  42. int bVerbose = FALSE;
  43. int  bBinary  = TRUE;
  44.  
  45. #define m_iswspace(a) (isspace(a) && (a) != '\n')
  46.  
  47. char *get_token( char *pch)
  48. {
  49.     char *pch2;
  50.  
  51.     while (*pch)
  52.         {
  53.         if (isalnum(*pch))
  54.             {
  55.             if (strchr( chFirstBuf, *pch))
  56.                 {
  57.                 pch2 = pch;
  58.                 while(isalnum(*pch2)) pch2++;
  59.                 token_length = (int)(pch2 - pch);
  60.                 return(pch);
  61.                 }
  62.             do
  63.                 {
  64.                 pch++;
  65.                 } while(isalnum(*pch));
  66.             }
  67.         while (*pch && !isalnum(*pch))
  68.             {
  69.             pch++;
  70.             } 
  71.         }
  72.     return(NULL);
  73. }
  74.  
  75. void print_dat()
  76. {
  77.     int ii, jj;
  78.  
  79.     for (ii = 0; ii < api_num; ii++)
  80.         {
  81.         printf( "Api Macro %s \n", apilist[ii].pchName);
  82.  
  83.         printf( "Macro Format:%s\n", apilist[ii].pchformat);
  84.         printf( "Parms:");
  85.         for ( jj = 0; jj < MAX_PARMS; jj++)
  86.             printf( " %d", apilist[ii].iOrder[jj]);
  87.         printf( "\n");
  88.  
  89.         }
  90.     printf( "First Letters: (%s)\n", chFirstBuf);
  91. }
  92.  
  93. void read_compiled()
  94.     {
  95.     int ii, jj;
  96.     char chTmpBuf[2];
  97.  
  98.     chFirstBuf[0] = 0x00;
  99.  
  100.  
  101.     fread( &api_num, sizeof(int), 1, fp_dat);
  102.  
  103.     for (ii = 0; ii < api_num; ii++)
  104.         {
  105.         fread( &apidump, sizeof(apidump), 1, fp_dat);
  106.  
  107.         if (strchr(chFirstBuf, apidump.pchName[0]) == NULL)
  108.             {
  109.             chTmpBuf[0] = apidump.pchName[0];
  110.             chTmpBuf[1] = 0x00;
  111.             strcat( chFirstBuf, chTmpBuf);
  112.             }
  113.  
  114.  
  115.         apilist[ii].cb         = apidump.cb;
  116.         apilist[ii].cParms     = apidump.cParms;
  117.  
  118.         if (apidump.pchName[0] != 0x00)
  119.             {
  120.             apilist[ii].pchName = malloc(strlen(apidump.pchName) + sizeof(char));
  121.             strcpy( apilist[ii].pchName, apidump.pchName);
  122.             }
  123.         else
  124.             apilist[ii].pchName = NULL;
  125.  
  126.         if (apidump.pchformat[0] != 0x00)
  127.             {
  128.             apilist[ii].pchformat = malloc(strlen(apidump.pchformat) + sizeof(char));
  129.             strcpy( apilist[ii].pchformat, apidump.pchformat);
  130.             }
  131.         else
  132.             apilist[ii].pchformat = NULL;
  133.  
  134.         for (jj = 0; jj < MAX_PARMS; jj++)
  135.             {
  136.             apilist[ii].iOrder[jj] = apidump.iOrder[jj];
  137.             }
  138.  
  139.         }
  140.  
  141.     }
  142.  
  143. void init_dat(int bDump)
  144. {
  145.     char chInBuf[256];
  146.     char chFmtBuf[256];
  147.     char chTmpBuf[256];
  148.     char pchParms[MAX_PARMS][34];
  149.     char ch;
  150.     char *pchOut;
  151.     char *pchIn;
  152.     int  iParmNo, iMatch;
  153.     int   nest = 0;
  154.     int ii;
  155.     long lftell = 0;
  156.     long *lptell = NULL;
  157.     struct _apitype *api;
  158.  
  159.  
  160.     if (bBinary)
  161.         {
  162.         read_compiled();
  163.         return;
  164.         }
  165.  
  166.  
  167.     chFirstBuf[0] = '\0';
  168.  
  169.     while (fgets( chInBuf, 256, fp_dat))
  170.         {
  171.         if (bDump)
  172.             fputs( chInBuf, fp_dst);
  173.  
  174.         if (chInBuf[0] == '@')
  175.             {
  176.  
  177.             api_num++;
  178.  
  179.             if (chInBuf[1] == '\n')
  180.                 return;
  181.                 
  182.             // printf( "%s", chInBuf + 1);
  183.  
  184.             if (strchr(chFirstBuf, chInBuf[1]) == NULL)
  185.                 {
  186.                 chTmpBuf[0] = chInBuf[1];
  187.                 chTmpBuf[1] = 0x00;
  188.                 strcat( chFirstBuf, chTmpBuf);
  189.                 }
  190.  
  191.             api = &apilist[api_num];
  192.             api->pchName = NULL;
  193.             api->pchformat = NULL;
  194.             api->cParms = 0;
  195.             lftell = ftell(fp_dat);
  196.  
  197.             for (ii = 0; ii < MAX_PARMS; ii++)
  198.                 {
  199.                 api->iOrder[ii] = -1;
  200.                 pchParms[ii][0] = 0x00;
  201.                 }
  202.  
  203.             pchOut = chTmpBuf;
  204.             pchIn  = chInBuf + 1;
  205.             assert( isalnum(*pchIn));
  206.  
  207.             while (isalnum(*pchIn))
  208.                 *pchOut++ = *pchIn++;
  209.  
  210.             *pchOut = 0x00;
  211.  
  212.             api->cb = strlen(chTmpBuf);
  213.             api->pchName = malloc(api->cb + sizeof(TCHAR));
  214.             strcpy(api->pchName, chTmpBuf);
  215.             if (bDump)
  216.                 printf("Replace::%s::\n", api->pchName);
  217.  
  218.             assert(*pchIn == '(');
  219.  
  220.             nest = 1;
  221.             pchIn++;
  222.             iParmNo = 0;
  223.  
  224.             while (nest)
  225.                 {
  226.                 pchOut = chTmpBuf;
  227.                 *pchOut = 0x00;
  228.  
  229.                 while (m_iswspace(*pchIn)) pchIn++;
  230.  
  231.                 while (isalnum(*pchIn))
  232.                     *pchOut++ = *pchIn++;
  233.                 *pchOut = 0x00;
  234.  
  235.                 // printf( "%s\n", chTmpBuf);
  236.  
  237.                 strcpy(pchParms[iParmNo++], chTmpBuf);
  238.                 if (*pchIn++ == ')')
  239.                     nest--;
  240.                 }
  241.             api->cParms = iParmNo;
  242.             // printf( "Parms %d\n", iParmNo);
  243.  
  244.             while (m_iswspace(*pchIn)) pchIn++;
  245.  
  246.             pchOut = chFmtBuf;
  247.  
  248.             if (*pchIn == '\n')
  249.                 {
  250.                 api->pchformat = NULL;
  251.                 if (bDump)
  252.                     printf( "Format::::\n" );
  253.                 continue;
  254.                 }
  255.  
  256.             assert( isalnum(*pchIn));
  257.  
  258.             while (isalnum(*pchIn) || *pchIn == '_')
  259.                 *pchOut++ = *pchIn++;
  260.  
  261.             if (*pchIn == '\n')
  262.                 {
  263.                 *pchOut = 0x00;
  264.                 api->pchformat = malloc(strlen(chFmtBuf) + sizeof(TCHAR));
  265.                 strcpy(api->pchformat, chFmtBuf);
  266.                 if (bDump)
  267.                     printf( "Format::%s::\n", api->pchformat);
  268.                 continue;
  269.                 }
  270.  
  271.             assert(*pchIn == '(');
  272.             *pchOut++ = *pchIn++;
  273.             *pchOut = 0x00;
  274.             // printf("line %d::%s\n", __LINE__, chFmtBuf );
  275.  
  276.             nest = 1;
  277.             iParmNo = 0;
  278.             iMatch = 0;
  279.  
  280.             while (nest)
  281.                 {
  282.                 pchOut = chTmpBuf;
  283.                 *pchOut = 0x00;
  284.  
  285.                 while (!isalnum(*pchIn) && nest > 0)
  286.                     {
  287.                     ch = *pchIn;
  288.                     *pchOut++ = *pchIn++;
  289.  
  290.                     if (ch == '(') nest++;
  291.                     if (ch == ')') nest--;
  292.                     if (ch == ',' ) iParmNo++;
  293.                     }
  294.  
  295.                 *pchOut = 0x00;
  296.                 strcat( chFmtBuf, chTmpBuf);
  297.                 // printf( "Parm %d, nest %d, Fmt::%s\n", iParmNo, 
  298.                 //        nest, chFmtBuf);
  299.  
  300.                 pchOut = chTmpBuf;
  301.                 *pchOut = 0x00;
  302.  
  303.                 if (nest == 0)
  304.                     continue;
  305.  
  306.                 while (isalnum(*pchIn))
  307.                     *pchOut++ = *pchIn++;
  308.                 *pchOut = 0x00;
  309.  
  310.                 // printf( "Token::%s\n", chTmpBuf);
  311.  
  312.                 for (ii = 0; ii < api->cParms; ii++)
  313.                     {
  314.                     // printf( "Compare (%s) and (%s)\n", chTmpBuf, 
  315.                     //    pchParms[ii]);
  316.                     if (strcmp(chTmpBuf, pchParms[ii]) == 0)
  317.                         {
  318.                         // printf( "Match Parm %d\n", ii);
  319.                         assert(api->iOrder[iMatch] == -1);
  320.                         api->iOrder[iMatch++] = ii;
  321.                         strcpy(chTmpBuf, "%s");
  322.                         }
  323.                     }
  324.                 strcat(chFmtBuf, chTmpBuf);
  325.  
  326.                 }
  327.             api->pchformat = malloc(strlen(chFmtBuf) + sizeof(TCHAR));
  328.             strcpy(api->pchformat, chFmtBuf);
  329.  
  330.             if (bDump)
  331.                 {
  332.                 printf( "Format::%s::", api->pchformat);
  333.                 for (ii = 0; ii < api->cParms + 1; ii++)
  334.                     printf( "%d, ", api->iOrder[ii]);
  335.                 printf("\n");
  336.                 }
  337.             }
  338.         else
  339.             {
  340.             if (lptell)
  341.                 {
  342.                 *lptell = lftell;
  343.                 lptell = NULL;
  344.                 }
  345.             }
  346.         }
  347. }
  348.  
  349. void add_buff(char *pch)
  350. {
  351.     char chExtraLine[256];
  352.     fgets( chExtraLine, 256, fp_src);
  353.     if (bVerbose)
  354.         fputs( chExtraLine, fp_err);
  355.  
  356.     strcat( pch, chExtraLine);
  357. }
  358.  
  359. void found( int index, char *pch)
  360. {
  361.     char chApiBuf[2048];
  362.     char chRestBuf[256];
  363.     char *pchIn = pch;
  364.     char *pchOut = chApiBuf;
  365.     char *pchEnd;
  366.     char pchParms[MAX_PARMS][256];
  367.     char *parmlist[MAX_PARMS];
  368.     struct _apitype *api = &apilist[index];
  369.     int nest = 0;
  370.     int iParmNo = 0;
  371.     int string = 0;
  372.     int ii;
  373.     char ch;
  374.  
  375.     for (ii = 0; ii < MAX_PARMS; ii++)
  376.         {
  377.         pchParms[ii][0] = 0x00;
  378.         parmlist[ii]    = NULL;
  379.         }
  380.  
  381.     strncpy( chApiBuf, pch, api->cb);
  382.     pchIn += api->cb;
  383.     pchOut = chApiBuf + api->cb;
  384.     while (iswspace(*pchIn)) pchIn++;
  385.     assert( *pchIn == '(');
  386.    *pchOut++ = *pchIn++;
  387.     nest = 1;
  388.  
  389.     while (nest > 0)
  390.         {
  391.         ch = *pchOut++ = *pchIn++;
  392.         if (ch == '(') nest++;
  393.         if (ch == ')') nest--;
  394.         if (ch == '\'' || ch == '\"')
  395.             {
  396.             char chEnd = ch;
  397.  
  398.             do
  399.                 {
  400.                 ch = *pchOut++ = *pchIn++;
  401.                 if (ch == '\\')
  402.                     ch = *pchOut++ = *pchIn++;
  403.                 if (ch == 0x00)
  404.                     {
  405.                     pchIn--;
  406.                     pchOut--;
  407.                     add_buff(pchIn);
  408.                     }
  409.                 }
  410.             while (ch != chEnd);
  411.             }
  412.         if (ch == 0x00)
  413.             {
  414.             pchIn--;
  415.             pchOut--;
  416.             add_buff(pchIn);
  417.             }
  418.         }
  419.  
  420.     *pchOut = 0x00;
  421.     strcpy( chRestBuf, pchIn);
  422.     pchIn = chApiBuf + api->cb + 1;
  423. //    printf( "Api (%s) Rest (%s)\n", chApiBuf, chRestBuf);
  424.     if (api->pchformat == NULL)
  425.         {
  426.         strcpy( pch, chRestBuf);
  427.         return;
  428.         }
  429.  
  430.     nest = 1;
  431.     iParmNo = 0;
  432.     pchEnd = pchIn;
  433.  
  434.     while (nest > 0)
  435.         {
  436.         int  cb;
  437.         char *pchBeginLoop = pchEnd;
  438.  
  439.         ch = *pchEnd;
  440.         while (isspace(ch) || isalnum(ch)
  441.             || strchr("&*.[]-></+|?!%=^_:", ch) ) ch = *++pchEnd;
  442.  
  443.         if ( ! (strchr("\"\'(),", ch)))
  444.             printf("Invalid character %c\n", ch);
  445.  
  446.         assert( ch != 0x00);
  447.  
  448.         if (ch == '\"' || ch == '\'')
  449.             {
  450.             char chEnd = ch;
  451.             do
  452.                 {
  453.                 ch = *++pchEnd;
  454.                 if (ch == '\\')
  455.                     ch = *++pchEnd;
  456.                 }
  457.             while (ch != chEnd);
  458.             ch = *++pchEnd;
  459.             }
  460.         if (ch == '(') 
  461.             {
  462.             nest++;
  463.             ++pchEnd;
  464.             }
  465.         if (ch == ')') 
  466.             {
  467.             nest--;
  468.             if (nest == 0)
  469.                 {
  470.                 cb = pchEnd - pchIn;
  471.                 strncpy(pchParms[iParmNo], pchIn, cb);
  472.                 pchParms[iParmNo++][cb] = 0x00;
  473.                 pchIn += cb + 1;
  474.                 pchEnd = pchIn;
  475.                 }
  476.             else
  477.                 ++pchEnd;
  478.             }
  479.         if (ch == ',' )
  480.             {
  481.             if (nest == 1)
  482.                 {
  483.                 cb = pchEnd - pchIn;
  484.                 strncpy(pchParms[iParmNo], pchIn, cb);
  485.                 pchParms[iParmNo++][cb] = 0x00;
  486.                 pchIn += cb + 1;
  487.                 pchEnd = pchIn;
  488.                 }
  489.             else
  490.                 ++pchEnd;
  491.             }
  492.         if(pchBeginLoop == pchEnd)
  493.             pchEnd++;
  494.         }
  495.  
  496.     for (ii = 0; ii < MAX_PARMS; ii++)
  497.         {
  498.         if (api->iOrder[ii] != -1)
  499.             parmlist[ii] = pchParms[api->iOrder[ii]];
  500.         else
  501.             parmlist[ii] = NULL;
  502.         }
  503.  
  504.     sprintf( pch, api->pchformat, parmlist[0], parmlist[1],
  505.         parmlist[2], parmlist[3], parmlist[4], parmlist[5],
  506.         parmlist[6], parmlist[7]);
  507.     strcat(pch, chRestBuf);
  508. }
  509.  
  510. char *FindApi( char *pch)
  511. {
  512.     int ii;
  513.     char *ptr;
  514.  
  515.     for (ii = 0; ii < api_num; ii++)
  516.         {
  517.         if ( token_length == apilist[ii].cb &&
  518.                 *pch == apilist[ii].pchName[0] &&
  519.                 (strncmp( pch, apilist[ii].pchName, apilist[ii].cb) == 0))
  520.             {
  521.             ptr = pch + apilist[ii].cb;
  522.             while (iswspace(*ptr)) ptr++;
  523.             if (*ptr == '(')
  524.                 {
  525.                 found(ii, pch);
  526.                 return( pch + 1);
  527.                 }
  528.             }
  529.         }
  530.  
  531.     return( pch + 1);
  532. }
  533.  
  534. void useage()
  535. {
  536.     printf( "Useage: ms_ssed [-v -d ] file_name [ file_result ] \n");
  537.     printf( "        -d -- dump data file. \n");
  538.     printf( "        -v -- Verbose, echo input to screen. \n");
  539. }
  540.  
  541. main( argc, argv)
  542.     int argc;
  543.     char *argv[];
  544.     {
  545.     int ii;
  546.     char src[30];
  547.     char dst[30];
  548.     char buffer[2560];
  549.     char *ptr;
  550.     int  bDump = FALSE;
  551.     char chBinFile[34] = "ms_ssed.bin";
  552.     char *pchBinFile = chBinFile;
  553.  
  554.     int  bCompile = FALSE;
  555.  
  556.     ii = 1;
  557.  
  558.     src[0] = 0x00;
  559.     dst[0] = 0x00;
  560.  
  561.     while (ii = my_getopt(argc, argv, "vd"))
  562.         {
  563.         switch (ii)
  564.             {
  565.             case ((int) 'd'):
  566.                 bDump = TRUE;
  567.                 break;
  568.             case ((int) 'v'):
  569.                 bVerbose = TRUE;
  570.                 break;
  571.  
  572.             case -1:
  573.                 if (src[0] == 0x00)
  574.                     {
  575.                     strcpy(src, p_optstr);
  576.                     break;
  577.                     }
  578.                 if (dst[0] == 0x00)
  579.                     {
  580.                     strcpy(dst, p_optstr);
  581.                     break;
  582.                     }
  583.  
  584.                 // FALL THRU
  585.  
  586.             default:
  587.                 useage();
  588.                 return(0);
  589.             }
  590.         }
  591.     
  592.     if (src[0] == 0x00 && bDump == FALSE )
  593.         {
  594.         useage();
  595.         return(0);
  596.         }
  597.  
  598.     fp_src = stdin;
  599.     fp_dst = stdout;
  600.     fp_err = stderr;
  601.  
  602.     fp_dat = fopen( pchBinFile, "rb");
  603.  
  604.     if (fp_dat == NULL)
  605.         {
  606.         printf( "Failed to open DataFile %s\n", pchBinFile);
  607.         return(0);
  608.         }
  609.  
  610.     if (bDump)
  611.         {
  612.         init_dat(TRUE);
  613.         print_dat();
  614.  
  615.         fclose(fp_dat);
  616.         return(0);
  617.         }
  618.  
  619.     if (dst[0] == 0x00)
  620.         {
  621.         strcpy( dst, src);
  622.         
  623.         if ((ptr = strchr( dst, '.')))
  624.             {
  625.             *ptr = '\0';
  626.             }
  627.         strcat( dst, ".win");   
  628.         }
  629.  
  630.     printf( "src: (%s) dst (%s)\n", src, dst);
  631.  
  632.     fp_src = freopen( src, "rt", stdin);
  633.     fp_dst = freopen( dst, "w", stdout);
  634.  
  635.     init_dat(FALSE);
  636.  
  637.     while (fgets( buffer, 256, fp_src))
  638.         {
  639.         if (bVerbose)
  640.             fputs( buffer, fp_err);
  641.  
  642.         ptr = buffer;
  643.         while (ptr = get_token( ptr))
  644.             {
  645.             ptr = FindApi(ptr);
  646.             }
  647.         fputs( buffer, fp_dst);
  648.         }
  649.     fclose(fp_dat);
  650.     fclose(fp_src);
  651.     fclose(fp_dst);
  652.     }
  653.  
  654.